
#include "MyComponent.h"
#import "MyWindowController.h"
#include <string>
#import "MainWindowDelegate.h"
#import "JpToolbar.h"

NS_IMPL_ISUPPORTS1(MyComponent, IMyComponent)


#define NS_SHOW_PANEL       "showpanel"
#define NS_SHOW_DIALOG      "showdialog"
#define NS_SHOW_TOOLTIPS    "showtooltips"

//static MainWindowDelegate * mainDelegate = NULL;

MyComponent::MyComponent(){
    NSLog(@"MyComponent::MyComponent");
    mainDelegate = NULL;
    
    toolbar = [[JpToolbar alloc] init];
    msgAction.setToolbar(toolbar);
}

MyComponent::~MyComponent(){
    
}


/* void OnToolbarActionByName (in string actionName, in string param); */
NS_IMETHODIMP MyComponent::OnToolbarActionByName(const char * actionName, const char * param)
{
    if (!actionName) {
        return NS_ERROR_NOT_IMPLEMENTED;
    }
    
    
//    callbackDecorator.GetBrowserWindow();
//    return NS_OK;
    
    
    std::string opName(actionName);
    if (opName.compare(NS_SHOW_PANEL) == 0) {
        
    }
    else if(opName.compare(NS_SHOW_DIALOG) == 0){
        msgAction.OnAction(actionName, param);
    }
    else if(opName.compare(NS_SHOW_TOOLTIPS) == 0){
//        MainWindowDelegate* temp = (MainWindowDelegate*)mainDelegate;
//        if (!mainDelegate) {
//            temp = [[MainWindowDelegate alloc] init];
//            mainDelegate = temp;
//            temp.callbackDecorator = (void*)&callbackDecorator;
//        }
//        
//        [temp showTooptips];
        
        NSBundle *curBundle = [NSBundle bundleForClass:[MyWindowController class]];
        NSString *curPath = [curBundle bundlePath];
        curPath = [curPath stringByAppendingString:@"/mybundle.bundle"];
        NSBundle *bundle = [[NSBundle alloc] initWithPath:curPath];
        
//        NSString* xibPath = [bundle pathForResource:@"MyWindow" ofType:@"nib"];

        
        NSMenu *menu = [[[NSMenu alloc] init] autorelease];
        //[NSMenu setMenuBarVisible:NO];
        [menu setAutoenablesItems:NO];
        {
            NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:@"58564100" action:@selector(menuResponse:) keyEquivalent:@""];
            item.tag = 1;
            NSString *path = [bundle pathForImageResource:@"notice.png"];
            NSImage *img = [[[NSImage alloc] initWithContentsOfFile:path] autorelease];
            [item setImage:img];
            [menu addItem:item];
            [item release];
        }
        {
            NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:@"龚俊杰" action:@selector(menuResponse:) keyEquivalent:@""];
            item.tag = 2;
            NSString *path = [bundle pathForImageResource:@"shop.png"];
            NSImage *img = [[[NSImage alloc] initWithContentsOfFile:path] autorelease];
            [item setImage:img];
            //[item setImage:[NSImage imageNamed:@"shop.png"]];
            [menu addItem:item];
            [item release];
        }
        {
            NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:@"Users" action:@selector(menuResponse:) keyEquivalent:@""];
            item.tag = 3;
            NSString *path = [bundle pathForImageResource:@"user.png"];
            NSImage *img = [[[NSImage alloc] initWithContentsOfFile:path] autorelease];
            [item setImage:img];
            //[item setImage:[NSImage imageNamed:@"user.png"]];
            [menu addItem:item];
            [item release];
        }
        
        {
            NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:@"News" action:@selector(menuResponse:) keyEquivalent:@""];
            item.tag = 4;
            NSString *path = [bundle pathForImageResource:@"mail_new.png"];
            NSImage *img = [[[NSImage alloc] initWithContentsOfFile:path] autorelease];
            [item setImage:img];
            //[item setImage:[NSImage imageNamed:@"mail_new.png"]];
            [menu addItem:item];
            [item release];
        }
        
        [menu addItem:[NSMenuItem separatorItem]];
        [menu addItemWithTitle:@"Exit" action:@selector(menuResponse:) keyEquivalent:@""]; 
        BOOL ret = [menu popUpMenuPositioningItem:nil 
                                       atLocation:NSMakePoint(300, 300)
                                           inView:nil];
        [bundle release];
    }
    else{
        return NS_ERROR_NOT_IMPLEMENTED;
    }
    
    return NS_OK;
}

/* void registerToolbarCallback (in nsToolbarCallback toolbarInterface); */
NS_IMETHODIMP MyComponent::RegisterToolbarCallback(nsToolbarCallback *toolbarInterface)
{
    //sleep(10);
    
//    NS_IMETHODIMP JpBrowserCallbackDecorator::GetTargetWindow(nsIBaseWindow** window)
//    {
//        if(NULL!=window && m_BrowserCallback)
//        {
//            return m_BrowserCallback->GetTargetWindow(window);
//        }
//        return NS_ERROR_NOT_IMPLEMENTED;
//    }
    
    if (!toolbarInterface) {
        return NS_ERROR_NOT_IMPLEMENTED;
    }
    
    
    callbackDecorator.setCallback(toolbarInterface);
    
    
    return NS_OK;
}



//NS_IMETHODIMP MyComponent::Add(PRInt32 a, PRInt32 b, PRInt32 *_retval NS_OUTPARAM){
//    *_retval = a + b;
//    
//    
//    MyWindowController *c = [[MyWindowController alloc] init];
//    NSWindow *p1 = c.window;
//    [c showWindow:nil];
//    
//    
//    //[c showMyDialog];
//    //[c showWindow:nil];
//    // show dialog
//    //[c showMyDialog];
//    
//    //[c release];
//    return NS_OK;
//}


